//
//  LoginViewController.m
//  DeepBreathing
//
//  Created by rimi on 15/12/15.
//  Copyright © 2015年 rimi. All rights reserved.
//

#import "LoginViewController.h"

#import "HelpViewController.h"

#import "RegisterViewContoller.h"

#import "LoginViewController.h"

#import "NetworkManger+User.h"

#import "UserModel.h"

#import "ForgetCodeViewController.h"

@interface LoginViewController ()
//输入处
@property(nonatomic,strong)UITextField *account;
@property(nonatomic,strong)UITextField *code;
@property(nonatomic,strong)UIImageView *accountImage;
@property(nonatomic,strong)UIImageView *codeImage;
//登录按捏
@property(nonatomic,strong)UIButton *loginButton;
//背景
@property(nonatomic,strong)UIImageView *backImage;

//底部
@property(nonatomic,strong)UIButton *registerButton;
@property(nonatomic,strong)UIButton *helpButton;
//忘记密码
@property(nonatomic,strong)UIButton *forgetButton;
//APP简介


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.navigationController setNavigationBarHidden:YES];
    
    _backImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, CGRectGetMaxY(self.view.frame)*2/3)];
    
    _backImage.image = [UIImage imageNamed:@"登录橘色背景图"];
    
    [self.view addSubview:_backImage];
    
    
    _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];

    _loginButton.frame = CGRectMake(CGRectGetMidX(self.view.frame) - 75, CGRectGetMaxY(self.view.frame)*2/3- 75, 150, 180);
    
    [_loginButton setImage:[UIImage imageNamed:@"登录按钮点击前"] forState:UIControlStateNormal];
    
    [_loginButton setImage:[UIImage imageNamed:@"登录按钮点击后"] forState:UIControlStateHighlighted];
    
    [_loginButton addTarget:self action:@selector(bordingToApp:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_loginButton];
    
    _accountImage = [[UIImageView alloc]initWithFrame:CGRectMake(50, CGRectGetMaxY(self.view.frame)/3 + 20 , 25, 25)];
    
    _accountImage.image = [UIImage imageNamed:@"登录用户名"];
    
    [self.view addSubview:_accountImage];
    
    _codeImage = [[UIImageView alloc]initWithFrame:CGRectMake(50, CGRectGetMaxY(self.view.frame)/3 + 70 , 25, 25)];
    
    _codeImage.image = [UIImage imageNamed:@"登录密码"];
    
    [self.view addSubview:_codeImage];
    
    _account = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_accountImage.frame) + 30, CGRectGetMaxY(self.view.frame)/3 + 10, 220, 40)];
    _account.placeholder = @"请输入账号";
    //
//    _account.text =@"13540107891";
    _account.font = [UIFont systemFontOfSize:16];
    [_account.layer setBorderWidth:2];
    
    [_account.layer setBorderColor:[UIColor whiteColor].CGColor];
    
    
    _account.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
    _account.leftViewMode = UITextFieldViewModeAlways;
    _account.textColor = [UIColor whiteColor];
    
    _account.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    _account.textAlignment = NSTextAlignmentLeft;
    
    [self.view addSubview:_account];
    
    _code = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_codeImage.frame) + 30, CGRectGetMaxY(self.view.frame)/3 + 60, 220, 40)];
    _code.placeholder = @"请输入密码";
    //
//    _code.text = @"yu1365810";
    _code.font = [UIFont systemFontOfSize:16];
    [_code.layer setBorderWidth:2];
    [_code.layer setBorderColor:[UIColor whiteColor].CGColor];
    _code.textColor = [UIColor whiteColor];
    
    _code.secureTextEntry =YES;
    
    _code.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    _code.textAlignment = NSTextAlignmentLeft;
    _code.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
    _code.leftViewMode = UITextFieldViewModeAlways;
    
    [self.view addSubview:_code];
    
    _registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [_registerButton setTitle:@"注册" forState:UIControlStateNormal];

    _registerButton.frame = CGRectMake(30, CGRectGetMaxY(self.view.frame) - 60, 60, 60);
    
    _registerButton.tintColor = [UIColor colorWithRed:0.9893 green:0.4175 blue:0.0318 alpha:0.83];
    
    _registerButton.titleLabel.font = [UIFont systemFontOfSize:20];
    
    [_registerButton addTarget:self action:@selector(pushToRegister:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_registerButton];
    
    _helpButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [_helpButton setTitle:@"帮助" forState:UIControlStateNormal];
    
    _helpButton.frame = CGRectMake(CGRectGetMaxX(self.view.frame) - 90, CGRectGetMaxY(self.view.frame) - 60, 60, 60);
    
    _helpButton.tintColor = [UIColor colorWithRed:0.9893 green:0.4175 blue:0.0318 alpha:0.83];

    
    _helpButton.titleLabel.font = [UIFont systemFontOfSize:20];
    
    [_helpButton addTarget:self action:@selector(pushToHelp:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_helpButton];

    
    _forgetButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    _forgetButton.frame = CGRectMake(CGRectGetMaxX(_codeImage.frame) + 30, CGRectGetMaxY(_code.frame) + 10, 80, 30);
    [_forgetButton setTitle:@"忘记密码？" forState:UIControlStateNormal];
    
    _forgetButton.tintColor = [UIColor whiteColor];
    [_forgetButton addTarget:self action:@selector(needHelp:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_forgetButton];
    
    NSArray *nameArray = @[@"深呼吸",@"慢性哮喘助手",@"Chronic Asthma Assistant"];
    
    for (int i = 0; i < 3; i ++) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame)- 120 , 70 + i * 40, 240, 40)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = nameArray[i];
        label.textColor = [UIColor whiteColor];
        
        if (i == 0) {
            label.font = [UIFont fontWithName:@"Helvetica-Bold" size:22];
        }else{
            label.font = [UIFont systemFontOfSize:20];
        }
        [self.view addSubview:label];
        
        
    }
    
    

}
#pragma mark -- private methods
-(void)bordingToApp:(UIButton *)sender{
    
                                [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
       [UserModel defaultUser].isLording = YES;
    

    NSString *phoneNuber = _account.text;
    NSString *codeNumber = _code.text;
   [NetworkManger loginWithUsername:_account.text password:_code.text successHander:^(id responseObject) {
//       NSLog(@"%@",responseObject[@"status"]);
//                            [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
//
       
       if ([responseObject[@"status"] isEqualToString:@"0"]) {
           
           [UserModel defaultUser].isLording = YES;
           [UserModel defaultUser].phoneNumber = phoneNuber;
           [UserModel defaultUser].password = codeNumber;
           [UserModel defaultUser].userID = responseObject[@"au"][@"id"];
//                      [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
           [self popuppromptBox:@"登录成功"];
                  }
       else if ([responseObject[@"status"] isEqualToString:@"-1"] && phoneNuber.length != 11){
           [self otherpopuppromptBox:@"请输入正确格式的手机号"];
       }else if(([responseObject[@"status"] isEqualToString:@"-1"]&& codeNumber.length < 6) || ([responseObject[@"status"] isEqualToString:@"-1"]&& codeNumber.length > 6)){
           
           [self otherpopuppromptBox:@"请输入6-16位密码"];
       }
       
   } failureHander:^(NSError *error) {
      
       
   }];
    
    
    
    

    
    
}

#pragma mark -- prompt box

-(void)otherpopuppromptBox:(NSString *)string{
    
    UIAlertController  *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}

-(void)popuppromptBox:(NSString *)string{
    
    UIAlertController  *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [UserModel defaultUser].isLording = YES;
        

        
    }];
    
    [alert addAction:action];
    [self.presentingViewController presentViewController:alert animated:YES completion:nil];
    
    
}
-(void)pushToRegister:(UIButton *)sender{
    
    RegisterViewContoller *RGVC = [[RegisterViewContoller alloc]init];
    
    [self.navigationController pushViewController:RGVC animated:YES];
    
}
-(void)pushToHelp:(UIButton *)sender{
    
    HelpViewController *HPVC = [[HelpViewController alloc]init];
    
    [self.navigationController pushViewController:HPVC animated:YES];
    
}
-(void)needHelp:(UIButton *)sender{
    
    ForgetCodeViewController *FGVC = [[ForgetCodeViewController alloc]init];
    
    [self.navigationController pushViewController:FGVC animated:YES];
    
}
@end
